﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.ModelBinding;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using PharmacyBussinesEntities;
using PharmacyService;

namespace PharmacyWeb.Almacen
{
    public partial class frmPresentacion : System.Web.UI.Page
    {
        [Dependency]
        public IPresentacionService PresentacionService { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            var accion = Request.QueryString["accion"];
            var idPresentacion = Request.QueryString["id"];

            if (accion != null && accion == "nuevo")
            {
                fvPresentacion.DefaultMode = FormViewMode.Insert;
            }
            else if (accion != null && accion == "eliminar" && idPresentacion != null)
            {
                var id = Int32.Parse(idPresentacion.ToString());

                PresentacionService.RemovePresentacion(id);
                Response.Redirect("frmListPresentacion.aspx");
            }
        }

        public Presentacion GetPresentacion([QueryString("identificador")] Int32? id)
        {
            Presentacion presentacion = null;
            if (id.HasValue)
                presentacion = PresentacionService.GetPresentacionById(id.Value);
            return presentacion;
        }

        public void UpdatePresentacion(Presentacion presentacion)
        {
            if (ModelState.IsValid)
            {
                PresentacionService.UpdatePresentacion(presentacion);
                Response.Redirect("frmListPresentacion.aspx");
            }
        }

        public void InsertPresentacion(Presentacion presentacion)
        {
            presentacion.Id = 0;
            if (ModelState.IsValid)
            {
                PresentacionService.AddPresentaion(presentacion);
                Response.Redirect("frmListPresentacion.aspx");
            }
        }
    }
}